package com.zorn.core.provider;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.springframework.web.context.ContextLoader;

public abstract class ServiceSupportProvider implements IServiceSupportProvider {

	private static final String SUCCESS = "success", TOTAL = "total", DATA = "data", MESSAGE = "message", AFFECTED = "affected";
	private static final String INT_REGEX = "[0-9]*";
	private Map<String, Object> result = new HashMap<String, Object>();

	// Methods
	public HttpServletRequest getRequest() {
		return ServletActionContext.getRequest();
	}

	public HttpServletResponse getResponse() {
		return ServletActionContext.getResponse();
	}

	public HttpSession getSession() {
		return getRequest().getSession();
	}

	public HttpSession getSession(boolean create) {
		return getRequest().getSession(create);
	}

	public ServletContext getServletContext() {
		return ServletActionContext.getServletContext();
	}

	public ServletContext getApplication() {
		return getServletContext();
	}

	public String getRealPath(String path) {
		return getServletContext().getRealPath(path);
	}

	@SuppressWarnings("unchecked")
	public Object getBean(Class cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public Object getBean(String cls) {
		return ContextLoader.getCurrentWebApplicationContext().getBean(cls);
	}

	public boolean isPaging() {
		return true;
		/*String start = getParameter("start", ""), limit = getParameter("limit", "");
		return start.matches(INT_REGEX) && limit.matches(INT_REGEX);*/
		// FIXME 放到参数拦截器去转换 这里只实现判断是否分页
		/*if (!result && getBooleanValue("Zorn.param.query.paging.AutoCorrectPagingParam", false)) {
			Map<String, Object> params = getParameterMap();
			params.put("start", Setting.getString("Zorn.param.query.paging.start", "0"));
			params.put("limit", Setting.getString("Zorn.param.query.paging.limit", "30"));
		}*/
	}

	public String getParameter(String name) {
		return getRequest().getParameter(name);
	}

	public String getParameter(String name, String def) {
		String param = getRequest().getParameter(name);

		if (param == null) return def;

		return param;
	}

	public String[] getParameterValues(String name) {
		return getRequest().getParameterValues(name);
	}

	public Map<String, Object> getResult() {
		return result;
	}

	/* Set Results
	==================================================================== */
	public void setResult(Map<String, Object> result) {
		this.result = result;
	}

	public void setResult(boolean success, String message) {
		setResult(success, "message", message);
	}

	public void setResult(boolean success, String key, Object value) {
		result = new HashMap<String, Object>(2);
		result.put("success", success);
		result.put(key, value);
	}

	public void setResult(boolean success, Object... contents) {
		result = new HashMap<String, Object>(contents.length / 2);
		for (int i = 0; i < contents.length; i++) {
			result.put(contents[i++].toString(), contents[i++]);
		}
	}

	public void setErrorResult(String message) {
		Map<String, Object> error = new HashMap<String, Object>();
		error.put("success", false);
		error.put("message", message);
	}

	public void setErrorResult(Map<String, Object> result) {
		setResult(result);
	}

	public List<Map<String, Object>> setQueryResult(int total, List<Map<String, Object>> data) {
		result = new HashMap<String, Object>();
		result.put("success", true);
		result.put("total", total);
		result.put("data", data);
		return data;
	}

	public List<Map<String, Object>> setQueryResult(List<Map<String, Object>> data, int total) {
		return data;
	}

	public int setCountResult(int count) {
		result = new HashMap<String, Object>(2);
		result.put(SUCCESS, true);
		result.put("count", count);
		return count;
	}

	public void setCountResult(boolean successful) {

	}

	public void setCountResult(boolean successful, int count) {

	}

	public void setCountResult(boolean successful, String message) {

	}

	public void setCountResult(boolean successful, String message, int count) {

	}

	public String setInsertResult(int affected, String id) {
		return id;
	}

	public void setInsertResult(String id) {

	}

	public void setInsertResult(boolean successful) {

	}

	public void setInsertResult(boolean successful, String message) {

	}

	public void setInsertResult(boolean successful, String message, int affected) {

	}

	public int setUpdateResult(int affected) {
		String message = null;
		if (affected == 0) {
			message = "已成功执行操作，但没有数据更新。";
		} else if (affected == 1) {
			message = "已成功更新 1 条数据。";
		} else if (affected > 1) {
			message = "已成功更新 " + affected + " 条数据。";
		} else {
			message = "已成功执行操作，数据状态异常。";
		}
		setUpdateResult(affected > 0, message, affected);
		return affected;
	}

	public void setUpdateResult(boolean successful) {

	}

	public void setUpdateResult(boolean successful, String message) {

	}

	public void setUpdateResult(boolean successful, String message, int affected) {
		Map<String, Object> result = new HashMap<String, Object>(3);
		result.put("success", successful);
		result.put("message", message);
		result.put("affected", affected);
		setResult(result);
	}

	public int setDeleteResult(int affected) {
		return affected;
	}

	public void setDeleteResult(boolean successful) {

	}

	public void setDeleteResult(boolean successful, String message) {

	}

	public void setDeleteResult(boolean successful, String message, int affected) {

	}

	// Getters and Setters

	/*public void setJSONProcessResult(boolean successful) {
		setJSONProcessResult(successful, null);
	}*/

	/*public void setJSONProcessResult(boolean successful, String message) {
		Map<String, Object> result = new HashMap<String, Object>();
		result.put("success", successful);
		result.put("message", message);
		this.result = result;
	}

	public void setJSONProcessResult(Map<String, Object> result) {
		this.result = result;
	}*/
}
